package com.jemmyee.task.web.controller.front;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.core.entity.Message;
import com.jemmyee.core.entity.User;
import com.jemmyee.core.web.Constants;
import com.jemmyee.core.web.utils.MathUtils;
import com.jemmyee.task.entity.UserNo;
import com.jemmyee.task.web.controller.kernel.BaseTaskFrontController;

/**
 * @Description: TODO
 * @author:jemmyee@gmail.com
 * @date:2011-9-1
 * @version:v1.0
 */
@Controller("taskMessageFrontController")
@RequestMapping(Constants.MV_URI_APP_TASK+Constants.MV_URI_MAIN_SSL+Constants.SYM_PATH_SEPARATOR+Constants.ENTITY_MESSAGE) 
public class MessageController extends BaseTaskFrontController{
	
	private static Log log= LogFactory.getLog(MessageController.class);
	/*分页查出保存的条件*/
	private static final String ENTITY_SESSION_BEAN="admin_session_bean_"+MathUtils.getRandomDouble();
	private static final String ROOT_VIEW=Constants.MV_URI_MAIN_FRONT+Constants.MV_URI_APP_TASK+Constants.SYM_PATH_SEPARATOR+Constants.ENTITY_MESSAGE;
	private static final String ROOT_URI=Constants.MV_URI_APP_TASK+Constants.MV_URI_MAIN_SSL+Constants.SYM_PATH_SEPARATOR+Constants.ENTITY_MESSAGE;
	
	/**
	 * @Description:信息操作界面
	 * @author:jemmyee@gmail.com
	 * @date:2011-9-6
	 */
	@RequestMapping("/toTabs/{tabID}")
	public ModelAndView toPointsTabs(@PathVariable int tabID,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("tabID",tabID);
		mv.addObject(Constants.MV_OBJECT_ROOT_URI, ROOT_URI);
		mv.setViewName(ROOT_VIEW+"/tabs"); 
		return mv;
	}
	
	@RequestMapping(Constants.MV_URI_COMM_ADD)
	@ResponseBody
	public Map<String, Object> add(@RequestBody Message bean,
			HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>();
		Message temp = null;
		try {
			if (messageService.findByProperty("title", bean.getTitle()) != null
					&& messageService.findByProperty("title", bean.getTitle())
							.size() > 0) {
				temp = messageService.findByProperty("title", bean.getTitle()).get(
						0);
			}
			if (temp != null && bean.getTitle().equals(bean.getTitle().trim())) {
				model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_RETITLE);
				return model;
			} else {
				//查找发送人ID
				List<User> users=userService.findByProperty("name",bean.getReserveField1());
				if(users.size()<0)//接手人不存在
				{
					model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_NOUSER);
					return model;
				}
				bean.setAddTime(new Date());
				bean.setIsDelete((short)0);
				bean.setFromId(getOnlineUser(request).getId());
				bean.setToId(users.get(0).getId());
				model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_SUCC);
				return model;
			}
		} catch (Exception e) {
			model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_ERROR);
			log.error(e);
		}
		return model;
	}
	
	/**
	* @Description: TODO
	* @author:jemmyee@gmail.com
	* @param type to from  del
	* @date:2011-9-8
	*/
	@RequestMapping(value ="/list/{tabID}/{from}/{pageSize}/{startIndex}")
	public ModelAndView list(@PathVariable Integer tabID,@PathVariable String from, @PathVariable Integer pageSize,
			@PathVariable Integer startIndex, @ModelAttribute Message bean,
			HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(Message.class);
		ModelAndView mv = new ModelAndView();
		Message temp = new Message();
		if (from.equals(Constants.PAGE_ENTRY_TYPE_FORM)) {
			temp = bean;
			request.getSession().setAttribute(ENTITY_SESSION_BEAN, bean);
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_PAGE)) {
			if (request.getSession().getAttribute(ENTITY_SESSION_BEAN) != null) {
				temp = (Message) request.getSession().getAttribute(
						ENTITY_SESSION_BEAN);
			}
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_MENU)) {

		}

		if (pageSize == 0) {
			pageSize = Constants.DEFAULT_PAGESIZE;
		}
		if(tabID.intValue()==1)//收
		{
			dc.add(Restrictions.eq("toId",getOnlineUser(request).getId()));
		}
		if(tabID.intValue()==2)//发
		{
			dc.add(Restrictions.eq("fromId",getOnlineUser(request).getId()));
		}
		if(tabID.intValue()==3)//垃圾
		{
			dc.add(Restrictions.eq("isDelete",(short)2));
		}
		dc.addOrder(Order.desc("addTime"));
		Page page = messageService.findPageByCriteria(dc, pageSize, startIndex);
		if (pageSize != 0) {
			page.setPageSize(pageSize);
		}
		String pageURI=request.getContextPath()+Constants.SYM_PATH_SEPARATOR+ROOT_URI+tabID;
		initList(mv,page,Constants.MV_VIEW_LIST,pageURI,ROOT_URI,temp,ROOT_VIEW,tabID,pageSize,startIndex);
		return mv;
	}

	
}
